<?php

namespace App\Models\Admin;

use App\Log\GoodsNonstandardAdminLog;
use App\Log\GoodsNonstandardLog;
use Illuminate\Database\Eloquent\Model;

class GoodsNonstandard extends Model
{
    const OPERATE_PID = 2;
    const MODEL = 'goods_nonstandard';

    protected $table = 'goods_nonstandard';

    protected $dateFormat = 'U';

    public $timestamps = true;

    protected $guarded = [];

    public function category()
    {
        return $this->hasOne(GoodsCategory::class,'id','category_id');
    }

    public function category_p()
    {
        return $this->hasOne(GoodsCategory::class,'id','pid');
    }

    public function spu()
    {
        return $this->hasMany(GoodsNonstandardSpu::class,'goods_nonstandard_id','id');
    }

    public function goods()
    {
        return $this->hasOne(Goods::class,'nonstandard_goods_id','id');
    }

    public function brandInfo()
    {
        return $this->hasOne(GoodsDatum::class,'id','brand');
    }

    public static function addLog($pre,$next,$type,$method='')
    {
        $info = AdminOperateModule::where('pid',self::OPERATE_PID)->where('type',$type)->where('model',self::MODEL)->when($method,function ($query)use ($method){
            $query->where('method',$method);
        })->first();
        if (!empty($info)){
            $adminLog = new GoodsNonstandardLog($pre,$next,$info);
            $adminLog->addLog();
        }
    }

}
